unit QExport4Clipboard;

{$I QExport4VerCtrl.inc}

interface

uses QExport4, Classes, QExport4Types;

const
  SDefaultViewer = 'Clipbrd.exe';

type
  TQClipboardExportType = (etSeparated, etFixed);

  TQExport4Clipboard = class(TQExport4Memory)
  private
    FSeparator: Char;
    FSpacing: Integer;
    FClipboardViewer: string;
    FExportType: TQClipboardExportType;
    procedure SetExportType(const Value: TQClipboardExportType);
  protected
    procedure BeginExport; override;
    procedure EndExport; override;
    function GetColCaption(Index: integer): string; override;
    function GetColData(ColValue: QEString;
      Column: TQExportColumn): QEString; override;
    function GetDataRow: QEString; override;
    procedure WriteDataRow; override;
    function GetCaptionRow: string; override;
    procedure WriteCaptionRow; override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property ExportType: TQClipboardExportType read FExportType write SetExportType
      default etSeparated;
    property Separator: Char read FSeparator write FSeparator;
    property Spacing: Integer read FSpacing write FSpacing default 2;
    property ClipboardViewer: string read FClipboardViewer write FClipboardViewer;

    property Header;
    property Footer;
    property AllowCaptions;
    property Captions;
    property Formats;
    property UserFormats;

    property OnFetchedRecord;
  end;


implementation

uses SysUtils, QExport4Common, QExport4EmsWideStrUtils
     {$IFDEF WIN32}
       , ClipBrd, Windows
     {$ENDIF}
     {$IFDEF LINUX}
       {$IFNDEF NOGUI}, QClipbrd{$ENDIF}
     {$ENDIF};

{ TQExport4Clipboard }

procedure TQExport4Clipboard.BeginExport;
begin
  inherited;
  AutoCalcColWidth := FExportType = etFixed;
  if Title <> EmptyStr then GetWriter.WriteLn(Title);
  if Header.Text <> EmptyStr then GetWriter.WriteLn(Header.Text);
end;

constructor TQExport4Clipboard.Create(AOwner: TComponent);
begin
  inherited;
  {$IFDEF QE_UNICODE}
  CharsetType := ectUTF16;
  {$ENDIF}
  FExportType := etSeparated;
  FSeparator := GetListSeparator;
  FSpacing := 2;
  FClipboardViewer := SDefaultViewer;
end;

procedure TQExport4Clipboard.EndExport;
begin
  if Footer.Text <> EmptyStr then GetWriter.WriteLn(Footer.Text);
  inherited;
end;

function TQExport4Clipboard.GetCaptionRow: string;
begin
  Result := inherited GetCaptionRow;
  if Length(Result) > 0 then
    Delete(Result, Length(Result), 1);
end;

function TQExport4Clipboard.GetColCaption(Index: integer): string;
begin
  Result := inherited GetColCaption(Index);
  if FExportType = etSeparated then Result := Result + FSeparator
  else Result := GetWriter.PadL(Result, ' ', Columns[Index].Width);
end;

function TQExport4Clipboard.GetColData(ColValue: QEString;
  Column: TQExportColumn): QEString;
begin
  Result := inherited GetColData(ColValue, Column);
  if FExportType = etSeparated then Result := Result + FSeparator
  else Result := GetWriter.PadL(Result, ' ', Column.Width);
end;

function TQExport4Clipboard.GetDataRow: QEString;
begin
  Result := inherited GetDataRow;
  if Length(Result) > 0 then
    QEDelete(Result, Length(Result), 1);
end;

procedure TQExport4Clipboard.SetExportType(const Value: TQClipboardExportType);
begin
  FExportType := Value;
  AutoCalcColWidth :=  FExportType = etFixed; 
end;

procedure TQExport4Clipboard.WriteCaptionRow;
begin
  GetWriter.WriteLn(GetCaptionRow);
end;

procedure TQExport4Clipboard.WriteDataRow;
begin
  GetWriter.WriteLn(GetDataRow);
end;

end.
